package view;

import controller.AdminCtrl;
import controller.addAdapter;
import controller.deleteAdapter;
import controller.updateAdapter;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import model.Group;

/**
 *
 * @author cska
 * 
 */

public class AdminWnd extends JFrame {

    protected GridBagConstraints c;
    protected JTabbedPane adminPane;
    protected JPanel groupTab;
    protected JList groupList;
    protected JButton addGroup, deleteGroup, updateGroup;
    protected JList studentList;
    protected JButton addStudent, deleteStudent, updateStudent;
    protected JPanel teacherTab;
    protected JTable teacherTable;
    protected JButton addTeacher, deleteTeacher, updateTeacher;
    protected JPanel subjectTab;
    protected JList subjectList;
    protected JButton addSubject, deleteSubject, updateSubject;
    protected JPanel gtsTab;
    protected JTable gtsTable;
    protected JButton addGts, deleteGts, updateGts;
    protected JPanel adminTab;
    protected JTable adminTable;
    protected JButton addAdmin, deleteAdmin, updateAdmin;
    protected JLabel l1, l2, l3, l4, l5, l6;
    protected JScrollPane sp1, sp2, sp3, sp4, sp5, sp6;
    protected AdminCtrl ctrl;
    protected String login;

    public AdminWnd(String login) {
        super();
        this.login = login;

        ctrl = new AdminCtrl();
        setLayout(new GridBagLayout());
        c = new GridBagConstraints(0, 0, 1, 1, 0.5, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        groupTab = new JPanel(new GridBagLayout());
        initGroupTab();

        teacherTab = new JPanel(new GridBagLayout());
        initTeacherTab();

        subjectTab = new JPanel(new GridBagLayout());
        initSubjectTab();

        gtsTab = new JPanel(new GridBagLayout());
        initGtsTab();

        adminTab = new JPanel(new GridBagLayout());
        initAdminTab();

        c = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        adminPane = new JTabbedPane();
        adminPane.addChangeListener(new tabChangedAdapter(this, adminPane));
        adminPane.add("Группы", groupTab);
        adminPane.add("Преподаватели", teacherTab);
        adminPane.add("Предметы", subjectTab);
        adminPane.add("Назначения", gtsTab);
        adminPane.add("Администраторы", adminTab);

        add(adminPane, c);

        adminPane.getChangeListeners()[0].stateChanged(null);

        setSize(640, 480);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width - 640) / 2, (screen.height - 480) / 2);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }

    public void initGroupTab() {
        c = new GridBagConstraints(0, 0, 1, 1, 0.5, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        c.gridwidth = 3;
        l1 = new JLabel("Список групп");
        l2 = new JLabel("Список студентов группы");
        l1.setHorizontalAlignment(JLabel.CENTER);
        l2.setHorizontalAlignment(JLabel.CENTER);
        c.weighty = 0.0;
        groupTab.add(l1, c);
        c.gridx = 3;
        groupTab.add(l2, c);

        groupList = new JList(ctrl.groupList());
        groupList.addListSelectionListener(new groupSelected(this));
        sp1 = new JScrollPane(groupList);
        sp1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        c.gridx = 0;
        c.gridy = 1;
        c.weighty = 1.0;
        groupTab.add(sp1, c);

        studentList = new JList();
        sp2 = new JScrollPane(studentList);
        sp2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        c.gridx = 3;
        c.gridy = 1;
        c.weighty = 1.0;
        groupTab.add(sp2, c);

        c.gridy = 2;
        c.gridwidth = 1;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.CENTER;
        addGroup = new JButton("Добавить");
        addGroup.addActionListener(new addAdapter(this, ctrl, addAdapter.ADD_GROUP));
        c.gridx = 0;
        c.weightx = 0.3;
        c.anchor = GridBagConstraints.EAST;
        groupTab.add(addGroup, c);
        deleteGroup = new JButton("Удалить");
        deleteGroup.addActionListener(new deleteAdapter(this, ctrl, deleteAdapter.DELETE_GROUP));
        c.gridx = 1;
        c.weightx = 0.0;
        c.anchor = GridBagConstraints.CENTER;
        groupTab.add(deleteGroup, c);
        updateGroup = new JButton("Изменить");
        updateGroup.addActionListener(new updateAdapter(this, ctrl, updateAdapter.UPDATE_GROUP));
        c.gridx = 2;
        c.weightx = 0.3;
        c.anchor = GridBagConstraints.WEST;
        groupTab.add(updateGroup, c);

        c.gridy = 2;
        c.gridwidth = 1;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.CENTER;
        addStudent = new JButton("Добавить");
        addStudent.addActionListener(new addAdapter(this, ctrl, addAdapter.ADD_STUDENT));
        c.gridx = 3;
        c.weightx = 0.3;
        c.anchor = GridBagConstraints.EAST;
        groupTab.add(addStudent, c);
        deleteStudent = new JButton("Удалить");
        deleteStudent.addActionListener(new deleteAdapter(this, ctrl, deleteAdapter.DELETE_STUDENT));
        c.gridx = 4;
        c.weightx = 0.0;
        c.anchor = GridBagConstraints.CENTER;
        groupTab.add(deleteStudent, c);
        updateStudent = new JButton("Изменить");
        updateStudent.addActionListener(new updateAdapter(this, ctrl, updateAdapter.UPDATE_STUDENT));
        c.gridx = 5;
        c.weightx = 0.3;
        c.anchor = GridBagConstraints.WEST;
        groupTab.add(updateStudent, c);
    }

    public void initTeacherTab() {
        c = new GridBagConstraints(0, 0, 1, 1, 0.5, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        c.gridwidth = 3;
        c.weighty = 0.0;
        l3 = new JLabel("Список преподавателей", JLabel.CENTER);
        teacherTab.add(l3, c);

        c.gridy = 1;
        c.weighty = 1.0;
        teacherTable = new JTable(ctrl.teacherTable()) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        sp3 = new JScrollPane(teacherTable);
        sp3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        teacherTab.add(sp3, c);

        c.gridy = 2;
        c.weighty = 0.0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.CENTER;

        c.gridx = 0;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.EAST;
        addTeacher = new JButton("Добавить");
        addTeacher.addActionListener(new addAdapter(this, ctrl, addAdapter.ADD_TEACHER));
        teacherTab.add(addTeacher, c);

        c.gridx = 1;
        c.weightx = 0.0;
        c.anchor = GridBagConstraints.CENTER;
        deleteTeacher = new JButton("Удалить");
        deleteTeacher.addActionListener(new deleteAdapter(this, ctrl, deleteAdapter.DELETE_TEACHER));
        teacherTab.add(deleteTeacher, c);

        c.gridx = 2;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.WEST;
        updateTeacher = new JButton("Изменить");
        updateTeacher.addActionListener(new updateAdapter(this, ctrl, updateAdapter.UPDATE_TEACHER));
        teacherTab.add(updateTeacher, c);

    }

    public void initSubjectTab() {
        c = new GridBagConstraints(0, 0, 1, 1, 0.5, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        c.gridwidth = 3;
        c.weighty = 0.0;
        l4 = new JLabel("Список предметов", JLabel.CENTER);
        subjectTab.add(l4, c);

        c.gridy = 1;
        c.weighty = 1.0;
        subjectList = new JList(ctrl.subjectList());
        sp4 = new JScrollPane(subjectList);
        sp4.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp4.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        subjectTab.add(sp4, c);

        c.gridy = 2;
        c.weighty = 0.0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.CENTER;

        c.gridx = 0;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.EAST;
        addSubject = new JButton("Добавить");
        addSubject.addActionListener(new addAdapter(this, ctrl, addAdapter.ADD_SUBJECT));
        subjectTab.add(addSubject, c);

        c.gridx = 1;
        c.weightx = 0.0;
        c.anchor = GridBagConstraints.CENTER;
        deleteSubject = new JButton("Удалить");
        deleteSubject.addActionListener(new deleteAdapter(this, ctrl, deleteAdapter.DELETE_SUBJECT));
        subjectTab.add(deleteSubject, c);

        c.gridx = 2;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.WEST;
        updateSubject = new JButton("Изменить");
        updateSubject.addActionListener(new updateAdapter(this, ctrl, updateAdapter.UPDATE_SUBJECT));
        subjectTab.add(updateSubject, c);

    }

    public void initGtsTab() {
        c = new GridBagConstraints(0, 0, 1, 1, 0.5, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        c.gridwidth = 3;
        c.weighty = 0.0;
        l5 = new JLabel("Список назначений", JLabel.CENTER);
        gtsTab.add(l5, c);

        c.gridy = 1;
        c.weighty = 1.0;
        gtsTable = new JTable(ctrl.gtsTable()) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        sp5 = new JScrollPane(gtsTable);
        sp5.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp5.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        gtsTab.add(sp5, c);

        c.gridy = 2;
        c.weighty = 0.0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.CENTER;

        c.gridx = 0;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.EAST;
        addGts = new JButton("Добавить");
        addGts.addActionListener(new addAdapter(this, ctrl, addAdapter.ADD_GTS));
        gtsTab.add(addGts, c);

        c.gridx = 1;
        c.weightx = 0.0;
        c.anchor = GridBagConstraints.CENTER;
        deleteGts = new JButton("Удалить");
        deleteGts.addActionListener(new deleteAdapter(this, ctrl, deleteAdapter.DELETE_GTS));
        gtsTab.add(deleteGts, c);

        c.gridx = 2;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.WEST;
        updateGts = new JButton("Изменить");
        updateGts.addActionListener(new updateAdapter(this, ctrl, updateAdapter.UPDATE_GTS));
        gtsTab.add(updateGts, c);
    }

    public void initAdminTab() {
        c = new GridBagConstraints(0, 0, 1, 1, 0.5, 1.0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

        c.gridwidth = 3;
        c.weighty = 0.0;
        l5 = new JLabel("Список назначений", JLabel.CENTER);
        adminTab.add(l5, c);

        c.gridy = 1;
        c.weighty = 1.0;
        adminTable = new JTable(ctrl.adminTable()) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        sp5 = new JScrollPane(adminTable);
        sp5.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp5.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        adminTab.add(sp5, c);

        c.gridy = 2;
        c.weighty = 0.0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.CENTER;

        c.gridx = 0;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.EAST;
        addAdmin = new JButton("Добавить");
        addAdmin.addActionListener(new addAdapter(this, ctrl, addAdapter.ADD_ADMIN));
        adminTab.add(addAdmin, c);

        c.gridx = 1;
        c.weightx = 0.0;
        c.anchor = GridBagConstraints.CENTER;
        deleteAdmin = new JButton("Удалить");
        deleteAdmin.addActionListener(new deleteAdapter(this, ctrl, deleteAdapter.DELETE_ADMIN));
        adminTab.add(deleteAdmin, c);

        c.gridx = 2;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.WEST;
        updateAdmin = new JButton("Изменить");
        updateAdmin.addActionListener(new updateAdapter(this, ctrl, updateAdapter.UPDATE_ADMIN));
        adminTab.add(updateAdmin, c);
    }

    class groupSelected implements ListSelectionListener {

        protected AdminWnd adminWnd;

        public groupSelected(AdminWnd adminWnd) {
            this.adminWnd = adminWnd;
        }

        public void valueChanged(ListSelectionEvent e) {
            if (adminWnd.getGroup() != -1) {
                adminWnd.reloadStudent();
            }
        }
    }

    class tabChangedAdapter implements ChangeListener {

        protected AdminWnd admin;
        protected JTabbedPane adminPane;

        public tabChangedAdapter(AdminWnd admin, JTabbedPane adminPane) {
            this.admin = admin;
            this.adminPane = adminPane;
        }

        public void stateChanged(ChangeEvent e) {
            int index = adminPane.getSelectedIndex();
            String s = "Администрирование :: ";
            switch (index) {
                case 0:
                    s += "Группы";
                    break;
                case 1:
                    s += "Преподаватели";
                    break;
                case 2:
                    s += "Предметы";
                    break;
                case 3:
                    s += "Назначения";
                    break;
                case 4:
                    s += "Администраторы";
                    break;

            }
            s+= " (Пользователь: "+admin.login+")";
            admin.setTitle(s);
        }
    }

    public int getGroup() {
        return groupList.getSelectedIndex();
    }

    public int getStudent() {
        return studentList.getSelectedIndex();
    }

    public int getTeacher() {
        return teacherTable.getSelectedRow();
    }

    public int getSubject() {
        return subjectList.getSelectedIndex();
    }

    public int getGts() {
        return gtsTable.getSelectedRow();
    }

    public int getAdmin() {
        return adminTable.getSelectedRow();
    }

    public void reloadGroup() {
        groupList.setModel(ctrl.groupList());
        reloadStudent();
        reloadGts();
    }

    public void reloadStudent() {
        studentList.setModel(ctrl.studentList(ctrl.getGroup(groupList.getSelectedIndex())));
    }

    public void reloadTeacher() {
        teacherTable.setModel(ctrl.teacherTable());
        reloadGts();
    }

    public void reloadSubject() {
        subjectList.setModel(ctrl.subjectList());
        reloadGts();
    }

    public void reloadGts() {
        gtsTable.setModel(ctrl.gtsTable());
    }

    public void reloadAdmin() {
        adminTable.setModel(ctrl.adminTable());
    }

}
